home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / myPlatform demo ƒ / sMovPlatform.c < prev    next >
Text File  |  1995-01-16  |  2KB  |  88 lines

  1. /* Platform sprite, moveable version, not faceless */
  2. /* */
  3.  
  4. #include "SAT.h"
  5. #include "myPlatform.h"
  6.  
  7. FacePtr    platFace;
  8.  
  9. void InitMovPlatform()
  10. {
  11.     platFace = SATGetFace(138);
  12. }
  13.  
  14. pascal void SetupMovPlatform(SpritePtr me)
  15. {
  16.     Rect            r;
  17.     PolyHandle    pol;
  18.     
  19.     me->speed.v =-1 + SATRand(2) * 2;
  20.     me->face = platFace; 
  21.     SetRect(&me->hotRect, 0, 3, 60, 20);
  22.     me->task = &HandleMovPlatform;
  23.     me->hitTask = &HitMovPlatform;
  24. }
  25.  
  26. pascal void HandleMovPlatform(SpritePtr me)
  27. {
  28.     me->position.v +=  me->speed.v;
  29.     if(me->position.v < 40)
  30.         me->speed.v =1;
  31.     if(me->position.v > gSAT.offSizeV - 32)
  32.         me->speed.v =-1;
  33.  
  34.     if(me->speed.v == 0){
  35.         if(me->position.v > gSAT.offSizeV/2)
  36.             me->speed.v = -1;
  37.         else
  38.             me->speed.v = 1;
  39.     }
  40.     me->layer = -1*(me->position.v);
  41.  
  42. }
  43.  
  44. pascal void HitMovPlatform(SpritePtr me, SpritePtr him)
  45. {
  46.     int    mini, i, min;
  47.     int    diff[5];
  48.         
  49.     if(him->task != HandlePlatform) {
  50.         diff[1] =-me->hotRect2.top + (him->hotRect2.bottom);        /* TtoB */
  51.         diff[2] =-him->hotRect2.top + (me->hotRect2.bottom);        /* BtoT */
  52.         diff[3] =-me->hotRect2.left + (him->hotRect2.right);            /* LtoR */
  53.         diff[4] =-him->hotRect2.left + (me->hotRect2.right);            /* RtoL */
  54.         mini = 0;
  55.         min = 10000;
  56.         for(i =1; i <= 4; i++)
  57.             if(min > diff[i]){
  58.                 min = diff[i];
  59.                 mini = i;
  60.             }
  61.         switch(mini){
  62.             case    1: 
  63.                     him->position.v = him->position.v - diff[1] + 2;  
  64.                     him->kind =10; 
  65.                     if(him->speed.v > 0)
  66.                         him->speed.v = 0;
  67.                     break;
  68.             case 2: 
  69.                     him->position.v = him->position.v + diff[2] + 1;
  70.                     if(him->speed.v < 0)
  71.                         him->speed.v = (him->speed.v)*(-1);
  72.                     break;
  73.             case 3: 
  74.                     him->position.h = him->position.h - diff[3] - 1;
  75.                     him->kind =10; 
  76.                     if(him->speed.h > 0)
  77.                         him->speed.h = (him->speed.h)*(-1);
  78.                     break;
  79.             case 4: 
  80.                     him->position.h =him->position.h + diff[4] + 1;
  81.                     him->kind =10; 
  82.                     if(him->speed.h < 0)
  83.                         him->speed.h = (him->speed.h)*(-1);
  84.                     break;
  85.         } /* switch */
  86.     }  /* if */
  87. }
  88.